<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Device extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'device';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_text',
        'panttime_text',
        'status_text'
    ];
    

    
    public function getTypeList()
    {
//        return ['online' => __('Type online'), 'offline' => __('Type offline'), 'water_warning' => __('Type water_warning'), 'ele_warning' => __('Type ele_warning'), 'all_faults' => __('Type all_faults')];
        return ['online' => __('Type online'), 'offline' => __('Type offline')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    public function getYjStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPanttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['panttime']) ? $data['panttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPanttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function ground()
    {
        return $this->belongsTo('Ground', 'ground_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
